package com.ruoyi.common.sdk.dxoos;

import com.ruoyi.common.utils.StringUtils;

/**
 * oos工具类
 * 富文本解析工具类
 */


public class RichTxtUtils {

    public static void main(String[] args) {
        String url = "legalbiz/2023-12-13/1f6a728f60bab70b5dda1e253c973b24 (1).jpeg";
        System.out.println(getNewUrl(url));
    }

    public static String getFileName(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return fileName;
        }
        //删除url临时链接认证
        if (fileName.indexOf("?") > 0) {
            fileName = fileName.substring(0, fileName.indexOf("?"));
        }
        //删除桶之类的相关信息
        fileName = fileName.replace(OosConfig.OOS_ENDPOINT + "/" + OosConfig.OOS_BUCKET_NAME + "/" + OosConfig.OOS_BUCKET_PATH + "/", "");
        return fileName;
    }

    public static String getUrlById(String key) {
        if (StringUtils.isEmpty(key)) {
            return key;
        }
        key = getFileName((key));
        String newUrl = "";
        //对私有桶进行获取分享链接操作
        try {
            newUrl = OosUtils.generatePresignedUrl(key, OosConfig.OOS_BUCKET_NAME + "/" + OosConfig.OOS_BUCKET_PATH, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newUrl;
    }

    public static String getNewUrl(String key) {
        if (StringUtils.isEmpty(key) || !key.contains(OosConfig.OOS_ENDPOINT)) {
            return key;
        }
        key = getFileName((key));
        String newUrl = "";
        //对私有桶进行获取分享链接操作
        try {
            newUrl = OosUtils.generatePresignedUrl(key, OosConfig.OOS_BUCKET_NAME + "/" + OosConfig.OOS_BUCKET_PATH, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newUrl;
    }

    public static String getNewUrlBatch(String image) {
        if (StringUtils.isNotBlank(image)) {
            String[] imageArr = image.split(",");
            if (null != imageArr && imageArr.length > 0) {
                StringBuilder newImage = new StringBuilder();
                for (String img : imageArr) {
                    if (!img.contains(OosConfig.HTTP) || img.contains(OosConfig.OOS_BUCKET_NAME_PATH)) {
                        newImage.append(RichTxtUtils.getNewUrl(img));
                        newImage.append(",");
                    }
                }
                if (StringUtils.isNotBlank(newImage)) {
                    newImage.setLength(newImage.length() - 1);
                    return newImage.toString();
                }
            }
        }
        return image;
    }

}
